

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>QEMU 与块设备 &mdash; Ceph Documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/ceph.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/ceph.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/css/custom.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/_sphinx_javascript_frameworks_compat.js"></script>
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/sphinx_highlight.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex/" />
    <link rel="search" title="Search" href="../../search/" />
    <link rel="next" title="通过 libvirt 使用 Ceph RBD" href="../libvirt/" />
    <link rel="prev" title="内核模块操作" href="../rbd-ko/" /> 
</head>

<body class="wy-body-for-nav">

   
  <header class="top-bar">
    <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="../">Ceph 块设备</a></li>
          <li class="breadcrumb-item"><a href="../rbd-integrations/">Ceph 块设备与第三方对接</a></li>
      <li class="breadcrumb-item active">QEMU 与块设备</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../../_sources/rbd/qemu-rbd.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
  </header>
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search"  style="background: #eee" >
          

          
            <a href="../../" class="icon icon-home"> Ceph
          

          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../start/">Ceph 简介</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../install/">安装 Ceph</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../cephadm/">Cephadm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../rados/">Ceph 存储集群</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../cephfs/">Ceph 文件系统</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../">Ceph 块设备</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../rados-rbd-cmds/">基本命令</a></li>
<li class="toctree-l2"><a class="reference internal" href="../rbd-operations/">运维</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../rbd-integrations/">对接</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="../rbd-ko/">内核模块</a></li>
<li class="toctree-l3 current"><a class="current reference internal" href="#">QEMU</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#id1">用法</a></li>
<li class="toctree-l4"><a class="reference internal" href="#id2">用 QEMU 创建映像</a></li>
<li class="toctree-l4"><a class="reference internal" href="#id3">用 QEMU 更改映像尺寸</a></li>
<li class="toctree-l4"><a class="reference internal" href="#id4">用 QEMU 检索映像信息</a></li>
<li class="toctree-l4"><a class="reference internal" href="#rbd-qemu">通过 RBD 运行 QEMU</a></li>
<li class="toctree-l4"><a class="reference internal" href="#discard-trim">启用 Discard/TRIM 功能</a></li>
<li class="toctree-l4"><a class="reference internal" href="#index-2">QEMU 缓存选项</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../libvirt/">libvirt</a></li>
<li class="toctree-l3"><a class="reference internal" href="../rbd-kubernetes/">Kubernetes</a></li>
<li class="toctree-l3"><a class="reference internal" href="../rbd-nomad/">Nomad</a></li>
<li class="toctree-l3"><a class="reference internal" href="../rbd-openstack/">OpenStack</a></li>
<li class="toctree-l3"><a class="reference internal" href="../rbd-cloudstack/">CloudStack</a></li>
<li class="toctree-l3"><a class="reference internal" href="../iscsi-overview/">LIO iSCSI Gateway</a></li>
<li class="toctree-l3"><a class="reference internal" href="../rbd-windows/">Windows</a></li>
<li class="toctree-l3"><a class="reference internal" href="../nvmeof-overview/">NVMe-oF 网关</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../man/">手册页</a></li>
<li class="toctree-l2"><a class="reference internal" href="../api/">APIs</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../radosgw/">Ceph 对象网关</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../mgr/">Ceph 管理器守护进程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../mgr/dashboard/">Ceph 仪表盘</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../monitoring/">监控概览</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../api/">API 文档</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../architecture/">体系结构</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../dev/developer_guide/">开发者指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../dev/internals/">Ceph 内幕</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../governance/">项目管理</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../foundation/">Ceph 基金会</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../ceph-volume/">ceph-volume</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../releases/general/">Ceph 版本（总目录）</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../releases/">Ceph 版本（索引）</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../security/">Security</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../hardware-monitoring/">硬件监控</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../glossary/">Ceph 术语</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../jaegertracing/">Tracing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../translation_cn/">中文版翻译资源</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../">Ceph</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
<div id="dev-warning" class="admonition note">
  <p class="first admonition-title">Notice</p>
  <p class="last">This document is for a development version of Ceph.</p>
</div>
  <div id="docubetter" align="right" style="padding: 5px; font-weight: bold;">
    <a href="https://pad.ceph.com/p/Report_Documentation_Bugs">Report a Documentation Bug</a>
  </div>

  
  <section id="qemu">
<h1>QEMU 与块设备<a class="headerlink" href="#qemu" title="Permalink to this heading"></a></h1>
<p id="index-0">Ceph 块设备最常见的用法之一是作为虚拟机的块设备映像。
例如，一用户可创建名为 golden 的映像，
并安装、配置好了操作系统和相关软件，
然后为此映像拍下快照，最后再克隆此快照
（可能很多次）。详情参见<a class="reference external" href="../rbd-snapshot/">快照</a>，
能制作快照的写时复制克隆品意味着 Ceph
可以快速地为虚拟机提供块设备映像，
因为客户端每次启动一个新虚拟机时不必下载整个映像。</p>
<p class="ditaa">
<img src="../../_images/ditaa-dcaaa7607d20f36f20db1319b8e10f6b420cef27.png"/>
</p>
<p>Ceph 块设备可以绑上 QEMU 虚拟机，关于 QEMU 见
<a class="reference external" href="http://wiki.qemu.org/Main_Page">QEMU 开源处理器仿真器</a>，其文档： <a class="reference external" href="http://wiki.qemu.org/Manual">QEMU 手册</a>。关于如何安装见<a class="reference external" href="../../install">安装</a>。</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>要让 QEMU 使用 Ceph 块设备，你必须有个运行着的
Ceph 集群。</p>
</div>
<section id="id1">
<h2>用法<a class="headerlink" href="#id1" title="Permalink to this heading"></a></h2>
<p>QEMU 命令行要求你指定 Ceph pool 和映像名字，
还可以指定一个快照名。</p>
<p>QEMU 会假设 Ceph 的配置文件位于默认位置
（如 <code class="docutils literal notranslate"><span class="pre">/etc/ceph/$cluster.conf</span></code> ）、并且你会以默认的 <code class="docutils literal notranslate"><span class="pre">client.admin</span></code> 用户执行命令，
除非你另外指定了其它 Ceph 配置文件路径或者其他用户。
指定用户时， QEMU 只需要 <code class="docutils literal notranslate"><span class="pre">ID</span></code> 部分，
无需完整地指定 <code class="docutils literal notranslate"><span class="pre">TYPE:ID</span></code> ，详情见<a class="reference external" href="../../rados/operations/user-management#user">用户管理——用户</a>。
别在用户 <code class="docutils literal notranslate"><span class="pre">ID</span></code> 前面加客户端类型（即 <code class="docutils literal notranslate"><span class="pre">client.</span></code> ），
否则认证会失败。还应该把 <code class="docutils literal notranslate"><span class="pre">admin</span></code> 用户、
或者你用 <code class="docutils literal notranslate"><span class="pre">:id={user}</span></code> 选项所指定用户的密钥保存到默认路径
（即 <code class="docutils literal notranslate"><span class="pre">/etc/ceph</span></code> ）或本地目录内的密钥环，
并修正密钥环文件的所有权和权限位。命令格式如下：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span><span class="o">-</span><span class="n">img</span> <span class="p">{</span><span class="n">command</span><span class="p">}</span> <span class="p">[</span><span class="n">options</span><span class="p">]</span> <span class="n">rbd</span><span class="p">:{</span><span class="n">pool</span><span class="o">-</span><span class="n">name</span><span class="p">}</span><span class="o">/</span><span class="p">{</span><span class="n">image</span><span class="o">-</span><span class="n">name</span><span class="p">}[</span><span class="nd">@snapshot</span><span class="o">-</span><span class="n">name</span><span class="p">][:</span><span class="n">option1</span><span class="o">=</span><span class="n">value1</span><span class="p">][:</span><span class="n">option2</span><span class="o">=</span><span class="n">value2</span><span class="o">...</span><span class="p">]</span>
</pre></div>
</div>
<p>例如，应该这样指定 <code class="docutils literal notranslate"><span class="pre">id</span></code> 和 <code class="docutils literal notranslate"><span class="pre">conf</span></code> 选项：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span><span class="o">-</span><span class="n">img</span> <span class="p">{</span><span class="n">command</span><span class="p">}</span> <span class="p">[</span><span class="n">options</span><span class="p">]</span> <span class="n">rbd</span><span class="p">:</span><span class="n">glance</span><span class="o">-</span><span class="n">pool</span><span class="o">/</span><span class="n">maipo</span><span class="p">:</span><span class="nb">id</span><span class="o">=</span><span class="n">glance</span><span class="p">:</span><span class="n">conf</span><span class="o">=/</span><span class="n">etc</span><span class="o">/</span><span class="n">ceph</span><span class="o">/</span><span class="n">ceph</span><span class="o">.</span><span class="n">conf</span>
</pre></div>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>配置中的值如果包含这些字符： <code class="docutils literal notranslate"><span class="pre">:</span></code> 、 <code class="docutils literal notranslate"><span class="pre">&#64;</span></code> 、 <code class="docutils literal notranslate"><span class="pre">=</span></code> ，
可在此符号前加反斜线 <code class="docutils literal notranslate"><span class="pre">\</span></code> 转义。</p>
</div>
</section>
<section id="id2">
<h2>用 QEMU 创建映像<a class="headerlink" href="#id2" title="Permalink to this heading"></a></h2>
<p>你可以用 QEMU 创建块设备映像。必须指定 <code class="docutils literal notranslate"><span class="pre">rbd</span></code> 、存储池名、
要创建的映像名以及映像尺寸。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span><span class="o">-</span><span class="n">img</span> <span class="n">create</span> <span class="o">-</span><span class="n">f</span> <span class="n">raw</span> <span class="n">rbd</span><span class="p">:{</span><span class="n">pool</span><span class="o">-</span><span class="n">name</span><span class="p">}</span><span class="o">/</span><span class="p">{</span><span class="n">image</span><span class="o">-</span><span class="n">name</span><span class="p">}</span> <span class="p">{</span><span class="n">size</span><span class="p">}</span>
</pre></div>
</div>
<p>例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span><span class="o">-</span><span class="n">img</span> <span class="n">create</span> <span class="o">-</span><span class="n">f</span> <span class="n">raw</span> <span class="n">rbd</span><span class="p">:</span><span class="n">data</span><span class="o">/</span><span class="n">foo</span> <span class="mi">10</span><span class="n">G</span>
</pre></div>
</div>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>其实 <code class="docutils literal notranslate"><span class="pre">raw</span></code> 数据格式是和 RBD 一起使用时唯一合理的
<code class="docutils literal notranslate"><span class="pre">format</span></code> 选项。从技术上讲，你可以用 QEMU 支持的其它格式
（像 <code class="docutils literal notranslate"><span class="pre">qcow2</span></code> 或 <code class="docutils literal notranslate"><span class="pre">vmdk</span></code> ），但是这样做会增加额外负担，
而且启用缓存后（见下文），
会使卷宗上的虚拟机实时迁移会变得不安全。</p>
</div>
</section>
<section id="id3">
<h2>用 QEMU 更改映像尺寸<a class="headerlink" href="#id3" title="Permalink to this heading"></a></h2>
<p>你可以通过 QEMU 调整块设备尺寸。必须指定 <code class="docutils literal notranslate"><span class="pre">rbd</span></code> 、
存储池名、要调整的映像名，还有映像尺寸。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span><span class="o">-</span><span class="n">img</span> <span class="n">resize</span> <span class="n">rbd</span><span class="p">:{</span><span class="n">pool</span><span class="o">-</span><span class="n">name</span><span class="p">}</span><span class="o">/</span><span class="p">{</span><span class="n">image</span><span class="o">-</span><span class="n">name</span><span class="p">}</span> <span class="p">{</span><span class="n">size</span><span class="p">}</span>
</pre></div>
</div>
<p>例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span><span class="o">-</span><span class="n">img</span> <span class="n">resize</span> <span class="n">rbd</span><span class="p">:</span><span class="n">data</span><span class="o">/</span><span class="n">foo</span> <span class="mi">10</span><span class="n">G</span>
</pre></div>
</div>
</section>
<section id="id4">
<h2>用 QEMU 检索映像信息<a class="headerlink" href="#id4" title="Permalink to this heading"></a></h2>
<p>你可以用 QEMU 检索块设备映像信息。必须指定 <code class="docutils literal notranslate"><span class="pre">rbd</span></code> 、存储池名、和映像名。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span><span class="o">-</span><span class="n">img</span> <span class="n">info</span> <span class="n">rbd</span><span class="p">:{</span><span class="n">pool</span><span class="o">-</span><span class="n">name</span><span class="p">}</span><span class="o">/</span><span class="p">{</span><span class="n">image</span><span class="o">-</span><span class="n">name</span><span class="p">}</span>
</pre></div>
</div>
<p>例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span><span class="o">-</span><span class="n">img</span> <span class="n">info</span> <span class="n">rbd</span><span class="p">:</span><span class="n">data</span><span class="o">/</span><span class="n">foo</span>
</pre></div>
</div>
</section>
<section id="rbd-qemu">
<h2>通过 RBD 运行 QEMU<a class="headerlink" href="#rbd-qemu" title="Permalink to this heading"></a></h2>
<p>QEMU 能把一主机上的块设备传递给访客，但从 QEMU 0.15 起，不需要在主机上把映像映射为块设备了。 QEMU 现在能直接通过
<code class="docutils literal notranslate"><span class="pre">librbd</span></code> 把映像捆绑成虚拟块设备了，这样性能更好，因为它避免了上下文切换，而且能利用<a class="reference external" href="../rbd-config-ref/#rbd-cache-config-settings">启用 RBD 缓存</a>带来的好处。</p>
<p>你可以用 <code class="docutils literal notranslate"><span class="pre">qemu-img</span></code> 把已有的虚拟机映像转换为
Ceph 块设备映像，比如你有一个 qcow2 映像，可以这样转：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span><span class="o">-</span><span class="n">img</span> <span class="n">convert</span> <span class="o">-</span><span class="n">f</span> <span class="n">qcow2</span> <span class="o">-</span><span class="n">O</span> <span class="n">raw</span> <span class="n">debian_squeeze</span><span class="o">.</span><span class="n">qcow2</span> <span class="n">rbd</span><span class="p">:</span><span class="n">data</span><span class="o">/</span><span class="n">squeeze</span>
</pre></div>
</div>
<p>要从那个映像引导虚拟机，执行：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span> <span class="o">-</span><span class="n">m</span> <span class="mi">1024</span> <span class="o">-</span><span class="n">drive</span> <span class="nb">format</span><span class="o">=</span><span class="n">raw</span><span class="p">,</span><span class="n">file</span><span class="o">=</span><span class="n">rbd</span><span class="p">:</span><span class="n">data</span><span class="o">/</span><span class="n">squeeze</span>
</pre></div>
</div>
<p><a class="reference external" href="../rbd-config-ref/#rbd-cache-config-settings">启用 RBD 缓存</a>可显著提升性能。从 QEMU 1.2 起，
缓存选项可控制 <code class="docutils literal notranslate"><span class="pre">librbd</span></code> 缓存：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span> <span class="o">-</span><span class="n">m</span> <span class="mi">1024</span> <span class="o">-</span><span class="n">drive</span> <span class="nb">format</span><span class="o">=</span><span class="n">rbd</span><span class="p">,</span><span class="n">file</span><span class="o">=</span><span class="n">rbd</span><span class="p">:</span><span class="n">data</span><span class="o">/</span><span class="n">squeeze</span><span class="p">,</span><span class="n">cache</span><span class="o">=</span><span class="n">writeback</span>
</pre></div>
</div>
<p>如果你的 QEMU 版本较老，你可以用 file 参数更改 librbd 缓存配置
（就像其它 Ceph 配置选项一样）：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span> <span class="o">-</span><span class="n">m</span> <span class="mi">1024</span> <span class="o">-</span><span class="n">drive</span> <span class="nb">format</span><span class="o">=</span><span class="n">raw</span><span class="p">,</span><span class="n">file</span><span class="o">=</span><span class="n">rbd</span><span class="p">:</span><span class="n">data</span><span class="o">/</span><span class="n">squeeze</span><span class="p">:</span><span class="n">rbd_cache</span><span class="o">=</span><span class="n">true</span><span class="p">,</span><span class="n">cache</span><span class="o">=</span><span class="n">writeback</span>
</pre></div>
</div>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>如果你设置了 rbd_cache=true ，那就必须设置 cache=writeback ，
否则有可能丢数据。没有 cache=writeback ，
QEMU 就不会向 librbd 发送回写请求，
如果 QEMU 退出时未清理干净， rbd 之上的文件系统就有可能被篡改。</p>
</div>
</section>
<section id="discard-trim">
<span id="index-1"></span><h2>启用 Discard/TRIM 功能<a class="headerlink" href="#discard-trim" title="Permalink to this heading"></a></h2>
<p>从 Ceph 0.46 和 QEMU 1.1 起， Ceph 块设备设备支持 discard 操作，这意味着访客可以发送 TRIM 请求来让 Ceph 块设备回收未使用的空间。此功能可在访客上挂载 <code class="docutils literal notranslate"><span class="pre">XFS</span></code> 或 <code class="docutils literal notranslate"><span class="pre">ext4</span></code> 时用 <code class="docutils literal notranslate"><span class="pre">discard</span></code> 选项启用。</p>
<p>要使此功能对访客可用，必须对块设备显式启用。
为此，你必须指定在驱动器上指定 <code class="docutils literal notranslate"><span class="pre">discard_granularity</span></code> ：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">qemu</span> <span class="o">-</span><span class="n">m</span> <span class="mi">1024</span> <span class="o">-</span><span class="n">drive</span> <span class="nb">format</span><span class="o">=</span><span class="n">raw</span><span class="p">,</span><span class="n">file</span><span class="o">=</span><span class="n">rbd</span><span class="p">:</span><span class="n">data</span><span class="o">/</span><span class="n">squeeze</span><span class="p">,</span><span class="nb">id</span><span class="o">=</span><span class="n">drive1</span><span class="p">,</span><span class="k">if</span><span class="o">=</span><span class="n">none</span> \
        <span class="o">-</span><span class="n">device</span> <span class="n">driver</span><span class="o">=</span><span class="n">ide</span><span class="o">-</span><span class="n">hd</span><span class="p">,</span><span class="n">drive</span><span class="o">=</span><span class="n">drive1</span><span class="p">,</span><span class="n">discard_granularity</span><span class="o">=</span><span class="mi">512</span>
</pre></div>
</div>
<p>注意，这个使用 IDE 驱动器， virtio 驱动从 Linux 内核版本 5.0 开始支持 discard 。</p>
<p>如果用的是 libvirt ，得用 <code class="docutils literal notranslate"><span class="pre">virsh</span> <span class="pre">edit</span></code> 编辑配置文件，
加上 <code class="docutils literal notranslate"><span class="pre">xmlns:qemu</span></code> 值。然后加一个 <code class="docutils literal notranslate"><span class="pre">qemu:commandline</span></code> 块作为那个域的子域。下例展示了如何用 <code class="docutils literal notranslate"><span class="pre">qemu</span> <span class="pre">id=</span></code>
为两个设备设置不同的 <code class="docutils literal notranslate"><span class="pre">discard_granularity</span></code> 值。</p>
<div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="nt">&lt;domain</span><span class="w"> </span><span class="na">type=</span><span class="s">&#39;kvm&#39;</span><span class="w"> </span><span class="na">xmlns:qemu=</span><span class="s">&#39;http://libvirt.org/schemas/domain/qemu/1.0&#39;</span><span class="nt">&gt;</span>
<span class="w">        </span><span class="nt">&lt;qemu:commandline&gt;</span>
<span class="w">                </span><span class="nt">&lt;qemu:arg</span><span class="w"> </span><span class="na">value=</span><span class="s">&#39;-set&#39;</span><span class="nt">/&gt;</span>
<span class="w">                </span><span class="nt">&lt;qemu:arg</span><span class="w"> </span><span class="na">value=</span><span class="s">&#39;block.scsi0-0-0.discard_granularity=4096&#39;</span><span class="nt">/&gt;</span>
<span class="w">                </span><span class="nt">&lt;qemu:arg</span><span class="w"> </span><span class="na">value=</span><span class="s">&#39;-set&#39;</span><span class="nt">/&gt;</span>
<span class="w">                </span><span class="nt">&lt;qemu:arg</span><span class="w"> </span><span class="na">value=</span><span class="s">&#39;block.scsi0-0-1.discard_granularity=65536&#39;</span><span class="nt">/&gt;</span>
<span class="w">        </span><span class="nt">&lt;/qemu:commandline&gt;</span>
<span class="nt">&lt;/domain&gt;</span>
</pre></div>
</div>
</section>
<section id="index-2">
<span id="id5"></span><h2>QEMU 缓存选项<a class="headerlink" href="#index-2" title="Permalink to this heading"></a></h2>
<p>QEMU 的缓存选项对应下列的 Ceph <a class="reference external" href="../rbd-config-ref/">RBD 缓存</a>选项。</p>
<p>回写：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">rbd_cache</span> <span class="o">=</span> <span class="n">true</span>
</pre></div>
</div>
<p>写透：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">rbd_cache</span> <span class="o">=</span> <span class="n">true</span>
<span class="n">rbd_cache_max_dirty</span> <span class="o">=</span> <span class="mi">0</span>
</pre></div>
</div>
<p>无：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">rbd_cache</span> <span class="o">=</span> <span class="n">false</span>
</pre></div>
</div>
<p>QEMU 的缓存选项会覆盖 Ceph 的缓存配置（包括 Ceph 配置文件里明确设置过的）。</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>在 QEMU v2.4.0 版以前，如果你在 Ceph 配置文件里明确设置了 <a class="reference external" href="../rbd-config-ref/">RBD 缓存</a>选项，
这些配置会覆盖 QEMU 的缓存选项。</p>
</div>
</section>
</section>



<div id="support-the-ceph-foundation" class="admonition note">
  <p class="first admonition-title">Brought to you by the Ceph Foundation</p>
  <p class="last">The Ceph Documentation is a community resource funded and hosted by the non-profit <a href="https://ceph.io/en/foundation/">Ceph Foundation</a>. If you would like to support this and our other efforts, please consider <a href="https://ceph.io/en/foundation/join/">joining now</a>.</p>
</div>


           </div>
           
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="../rbd-ko/" class="btn btn-neutral float-left" title="内核模块操作" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="../libvirt/" class="btn btn-neutral float-right" title="通过 libvirt 使用 Ceph RBD" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2016, Ceph authors and contributors. Licensed under Creative Commons Attribution Share Alike 3.0 (CC-BY-SA-3.0).</p>
  </div>

   

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>